<?php
declare (strict_types=1);

namespace app\api\controller;

use app\common\model\Assets;
use app\common\model\Account as AccountModel;
use app\common\tool\Result;
use think\facade\Db;
use think\Response;

class Account extends Base
{
    public function index(): Response
    {
        $param = $this->request->get();
        $list = Assets::getFields(3);
        $type = $param['type']?? 1;
        $result = [];
        switch ($type) {
            case 1:
                $account = ['currency', 'red_coin', 'chain_bean', 'love_bean', 'vip_integ', 'video_reward_surplus'];
                break;
            case 2:
                $account = ['rabbit_coupon', 'ranch_points', 'ranch_bonus'];
                break;
            case 3:
                $account = ['subsidy', 'dedicate'];
                break;
            case 4:
                $account = ['rabbit_vote', 'rabbit_chain', 'rabbit_baby', 'cloud_points'];
                break;
        }
        foreach ($list as $key => $v) {
            if (in_array($key, $account)) {
                $result[] = [
                    'title' => $v,
                    'account' => $key,
                    'balance' => $this->user->assets->$key,
                    'total' => AccountModel::where(['uid'=>$this->user->id, 'type'=> $key, 'fx'=>'+'])->sum('amount')
                ];
            }
        }
        if ($type == 2) {
            $balance = Db::query('SELECT sum(`rebate_amount_sum`-`rebate_amount`) as `balance` FROM `pasture_release` where `uid` = '.$this->user->id.' and is_del = 0');
            $result[] = [
                'title' => '预估收益',
                'account' => 'estimate_income',
                'balance' => $balance[0]['balance']?:0
            ];
        }
        if ($type == 4) {
            $balance = Db::query('SELECT sum(`rebate_amount_sum`-`rebate_amount`) as `balance` FROM `tillage_release` where `uid` = '.$this->user->id.' and is_del = 0');
            $result[] = [
                'title' => '预估收益',
                'account' => 'estimate_income',
                'balance' => $balance[0]['balance']?:0
            ];
        }
        /*elseif ($type == 3) {
            $result[] = [
                'title' => '贡献值',
                'account' => 'dedicate',
                'balance' => $this->user->my_dedicate
            ];
        }*/

        return Result::success('success', $result);
    }

    /**
     * 账户流水
     * @return Response
     */
    public function getList(): Response
    {
        $param = $this->request->param();
        $map = ['type' => $param['type'], 'uid' => $this->user->id];

        $sort = [
            'key' => 'id',
            'value' => 'desc'
        ];
        return Result::fastReturn(AccountModel::getPageList($map, 'id,intro,amount,after,create_time,fx', $this->getPage(), $this->getLimit(), false, ['take'], [], $sort));
    }
}
